package md.lanjing.com.module_deal_center.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lxh.baselibray.base.XFragment;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.view.TitleView;

import butterknife.BindView;
import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.ui.activity.OrderActivity;


/**
 * 交易中心
 */
public class DealCenterFragment extends XFragment {


    @BindView(R2.id.titleView)
    TitleView titleView;
    @BindView(R2.id.rb_buy)
    RadioButton rbBuy;
    @BindView(R2.id.rb_consign)
    RadioButton rbConsign;
    @BindView(R2.id.rg)
    RadioGroup rg;
    @BindView(R2.id.fragment_container)
    FrameLayout fragmentContainer;
    private Fragment showFragment;

    @Override
    public void initData(Bundle savedInstanceState) {
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_buy) {
                fragmentManager(R.id.fragment_container, new BuyFragment(), "buy");

            } else if (checkedId == R.id.rb_consign) {
                fragmentManager(R.id.fragment_container,new ConsignFragment(), "consign");

            }
        });
        rg.getChildAt(0).performClick();

        titleView.setRightOnclickListener(v -> {
            startActivity(new Intent(context, OrderActivity.class));
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_deal_center;
    }

    private void fragmentManager(int resid, Fragment fragment, String tag) {
        FragmentManager fragmentManager =getActivity().getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //隐藏正在显示的Fragment对象
        if (showFragment != null) {
            fragmentTransaction.hide(showFragment);
        }

        Fragment mFragment = fragmentManager.findFragmentByTag(tag);
        if (mFragment != null) {
            fragmentTransaction.show(mFragment);
        } else {
            mFragment = fragment;
            fragmentTransaction.add(resid, mFragment, tag);
        }

        showFragment = mFragment;
        fragmentTransaction.commit();
    }
}
